<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  Edit Offer Handler (/admin/handler.offer_edit.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require dirname(__FILE__) . "/../config.php";

// Require coupon generator function
require_once(dirname(__FILE__)."/function.coupon_generate.php");

// Start session if not already started
session_start();

if(!isset($_SESSION['login_t'])) {
	/* If we're not logged in, take us to the login
	   page and ensure the rest of the code isn't run */
	header("Location: /admin/login.php");
	exit;
}

$pagetitle = "Edit Offer (Admin)";
$pageheading = "<a href=\"/admin/index.php\">" . SITE_NAME . " Administration</a> &gt; Edit Offer";
$page_content = "<hr />\n";

if(!isset($_POST['campaign_id']) || !is_numeric($_POST['campaign_id'])) {
	// No campaign to add offer to
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No campaign was selected for editing an offer. Select a campaign by clicking its name on the List Campaigns page.</div>\n";
} else if(!isset($_POST['offer_id']) || !is_numeric($_POST['offer_id'])) {
	// No campaign to add offer to
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No offer was selected for editing. Select an offer by clicking its respective \"Edit\" icon on the List Offers page.</div>\n";
} else if ($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_POST['campaign_id'] . "'") == false) {
	// Non-existent campaign
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The campaign selected does not exist. Please select another offer for editing by using its respective \"Edit\" icon on the List Offers page.</div>\n";
} else if ($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "offers` WHERE `offer_id` = '" . $_POST['offer_id'] . "'") == false) {
	// Non-existent offer
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The offer selected does not exist. Please select another offer for editing by using its respective \"Edit\" icon on the List Offers page.</div>\n";
} else {
	
	$campaign = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_POST['campaign_id'] . "'");
	$offer = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "offers` WHERE `offer_id` = '" . $_POST['offer_id'] . "'");
	// Update existing offer
	$db->query("UPDATE `" . DB_TBL_PREFIX . "offers` SET offer_line1 = '". $_POST['offer_line1'] ."', offer_line2 = '". $_POST['offer_line2'] ."', offer_line3 = '". $_POST['offer_line3'] ."', offer_pos_code = '". $_POST['offer_pos_code'] ."', offer_type = '". $_POST['offer_type'] ."', offer_price = '". $_POST['offer_price'] ."', offer_value = '". $_POST['offer_value'] ."' WHERE `offer_id` = '". $_POST['offer_id'] . "'");
	
	foreach($_POST as $key=>$val) {
		if($key != 'campaign_id' || $key != 'offer_id') $off[$key] = $val;
		$_SESSION[$key] = $val;
	}
	$off['offer_end_date'] = $campaign->end_date;
	$offer_img = coupon_generate($off, $coupon);
	$fname = CACHE_DIR . "/" . $campaign->campaign_id . "_" . $offer->offer_id . ".png";
	if(file_exists($fname)) {
		unlink($fname);
	}
	file_put_contents($fname, $offer_img);
	$page_content .= "<div id=\"success-box\">Offer #" . $offer->offer_id . " was successfully edited.</div>\n";
}

$page_content .= "<p class=\"centered\"><a href=\"/admin/list_campaigns.php\">Back</a> to Campaign List</p>\n";
$page_content .= "<hr />\n";
$page_content .= "<p><a href=\"/admin/index.php\">Back</a> to " . SITE_NAME . " Administration</p>\n";


// Get template
require(TEMPLATE_DIR . "/index.php");
?>
